<?php

class Bt_rozetka_com_ua extends Grabber_Abstract
{
	protected $_count_grab_news = 1;
	protected $_translate_text = false;

	protected function _parsingList( $html )
	{
		$links = array();
		$html = $this->_cutSpaces($html);

		$matched = $this->_match('<table\s+class="item\s.*".*>.*<td.*>.*[\s\t\r\n.]*.*<img.*alt="(.*)".*[\s\t\r\n.]*.*<table\s.*>.*<a\shref="(.*)".*>([1-9][0-9]*)\s.*</a>', $html);
		
		if( !empty( $matched[1] ) && !empty( $matched[2] ) )
		{
			foreach( $matched[1] as $key => $value )
			{
				$links[$matched[1][$key]] = $matched[2][$key];
			}
		}

		return $links;
	}
	
	protected function _nextListPage( $html )
	{
		$html = $this->_cutSpaces($html);
		
		$matched = $this->_match('<td\s+class="next"><a\s+href="(.*)".*>.*</a>', $this->_cutSpaces($html));
	
		if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
		{
			return $matched[1][0];
		}
		
		return false;
	}
	
	/**
	 * Парсит html, витягивает полную статью
	 *
	 * @param type $html - string
	 * @return array('news'=>string, 'comments'=>array());
	 */
	protected function _getNews( $html, $news_row )
	{
		$result = array( 'news' => '', 'title' => '', 'comments' => array() );
		
		$html = $this->_cutSpaces( $html );

		$result['title'] = $news_row->title;
		$result['news'] = '';
		
		$result['comments'] = array();

		$this->_getComments($html, $result['comments']);
		
		if( $result['comments'] )
		{
			$result['news'] = current($result['comments']);
			unset( $result['comments'][key($result['comments'])] );
		}
		
		return $result;
	}
	
	protected function _getComments( $html, &$comments )
	{
		$matched = $this->_match('<li\sname="\d*">.*</p>(<p>.*</p>)<div', $html);

		if( !empty( $matched[1] ) )
		{
			foreach( $matched[1] as $comment )
			{
				$comments[] = $comment;
			}
		}
		
		$matched = $this->_match('<td name="navigation_next".*><a\sname=".*"\shref="(.*)"', $html);
		
		if( isset($matched[1]) && !empty($matched[1][0]) )
		{
			$html = $this->_oBrowser->get( $matched[1][0] );

			if( $this->_convertToUtf8 )
			{
				$html = $this->_toUtf8( $html, $this->_convertToUtf8 );
			}
			$html = $this->_cutSpaces( $html );
			
			$this->_getComments( $html, $comments );
		}
	}
	
}
